package com.qdlc.p2p.biz.sms;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.qdlc.p2p.common.core.PropertyPlaceholderConfigurerHelper;
import com.qdlc.p2p.dal.dto.NoticeType;

/**
 * 选择短信提供商，并发送短信，获取返回结果
 * @author linya 2016-01-21
 *
 *只是用一个默认的短信通道，原先代码保留，便于以后再加短信通道
 *@author linya 2016-01-22
 */
public class SmsProtalProviderChooser {

	//定义所有的短信通道集合
	private static final List<SmsPortal> smsProtalList = new ArrayList<SmsPortal>();
	
	//定义默认使用的短信通道
	private static final SmsPortal DEFAUT_SMS_PROTAL = new EmaySmsPortalImpl();
	
	static{
        smsProtalList.add(DEFAUT_SMS_PROTAL);
	}
	
	/**
	 * 从配置的sendRoute中选择出提供的短信提供商
	 * sendRoute的格式为:  
	 * 例如：  sendRoute="6:4"，则表示选择第6通道和第4通道发送短信
	 * @param sendRoute
	 * @return
	 * @see NoticeType#getSendRoute()
	 */
	public static SmsProtalProviderChain choice(String sendRoute){
		return noChoice();
	}
	
	private static SmsProtalProviderChain noChoice(){
		return choice0(null);
	}
	
	private static SmsProtalProviderChain choice0(String sendRoute){
		//构将短信提供商链式对象
		SmsProtalProviderChain chain = new SmsProtalProviderChain();
		if(StringUtils.isBlank(sendRoute)){
			return chain;
		}
		//可选的短信提供商
		List<SmsPortal> choiceSmsProtals = new ArrayList<SmsPortal>();
		//根据路由获取提供商
		String[] routeArray = sendRoute.split(":");
		for(String route: routeArray){
			try {
				Integer index = Integer.valueOf(route);
				index = Math.max(index-1, 0);
				SmsPortal choise = smsProtalList.get(index);
				choiceSmsProtals.add(choise);
			} catch (NumberFormatException e) {
				//ignore
			}
		}
		chain.setChoiseSmsProtals(choiceSmsProtals);
		return chain;		
	}
	
	public static class SmsProtalProviderChain{
		
		private static final String RESULT_SUCCESS = "ok";
		
		private List<SmsPortal> choiseSmsProtals = null;
		
		public SmsResult send(String moblie, String content){
			
			//如果没有提供商，则使用默认
			if(choiseSmsProtals == null || choiseSmsProtals.size() == 0){
				choiseSmsProtals = new ArrayList<SmsPortal>();
				choiseSmsProtals.add(DEFAUT_SMS_PROTAL);
			}
			
			//遍历发送短信，一旦成功，则返回结果
			SmsResult smsResult = new SmsResult();
			List<String> failureMessage = new ArrayList<String>();
			//提供测试时不真实发送短信用
			boolean smsEnabled = PropertyPlaceholderConfigurerHelper.getBooleanValue("sms.enabled");
			for(SmsPortal smsProtal: choiseSmsProtals){
				String result = RESULT_SUCCESS;
				if(smsEnabled){
					result = smsProtal.send(moblie, content);
				}
				if(RESULT_SUCCESS.equals(result)){
					smsResult.setSuccess(true);
					System.out.println("++++ 发送短信:" + content);
					smsResult.setMessage("通过短信通道" + smsProtal.getSPName() + "发送短信成功。");
					break;
				}else{
					failureMessage.add(smsProtal.getSPName() + ":" + result);
				}
			}
			
			if(!smsResult.isSuccess()){
				smsResult.setMessage(Arrays.toString(failureMessage.toArray()));
			}
			
			return smsResult;
		}

		protected void setChoiseSmsProtals(List<SmsPortal> choiseSmsProtals) {
			this.choiseSmsProtals = choiseSmsProtals;
		}
		
	}
	
	/**
	 * 发送短信，返回的结果
	 * @author linya
	 */
	public static class SmsResult{
		
		boolean success = false;
		
		String message;
		
		public boolean isSuccess() {
			return success;
		}
		
		protected void setSuccess(boolean success) {
			this.success = success;
		}
		
		public String getMessage() {
			return message;
		}
		
		protected void setMessage(String message) {
			this.message = message;
		}
	}
}
